package com.qinggeng.project.system.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.qinggeng.framework.web.controller.BaseController;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.framework.web.page.TableDataInfo;
import com.qinggeng.project.system.domain.GoodsStatistics;
import com.qinggeng.project.system.domain.OrderStatistics;
import com.qinggeng.project.system.service.IGoodsStatisticsService;
import com.qinggeng.project.system.service.IOrderStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/goodsStatistics")
public class GoodsStatisticsController extends BaseController {

    @Autowired
    private IGoodsStatisticsService goodsStatisticsService;

    @Autowired
    private IOrderStatisticsService orderStatisticsService;

    /**
     * 单商品销量统计
     * @param shopId
     * @param classifyId
     * @param startTime
     * @param endTime
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:goodsStatistics:list')")
    @GetMapping("/list")
    public TableDataInfo selectGoodsStatisticsList(Long shopId,Long pointId,Long classifyId,Long startTime,Long endTime)    {
        List<GoodsStatistics> list = goodsStatisticsService.selectGoodsStatisticsList(shopId,pointId,classifyId,startTime,endTime);
        return getDataTable(list);
    }

    /**
     * 根据商品一级分类统计各分类商品销量
     * @param shopId
     * @param startTime
     * @param endTime
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:goodsStatistics:classify')")
    @GetMapping("/classify")
    public TableDataInfo selectGoodsStatisticsListByClassifyId(Long shopId,Long startTime, Long endTime)    {
        List<GoodsStatistics> list = goodsStatisticsService.selectGoodsStatisticsListByClassifyId(shopId,null,startTime,endTime);
        return getDataTable(list);
    }

    /**
     * 根据配送点ID统计该配点各商品销量
     * @param shopId
     * @param pointId
     * @param classifyId
     * @param index
     * @param startTime
     * @param endTime
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:goodsStatistics:point')")
    @GetMapping("/point")
    public AjaxResult selectGoodsStatisticsListByPointId(Long shopId, Long pointId, Long classifyId,@RequestParam(defaultValue = "10") Integer index, Long startTime, Long endTime)    {
        Map<String,Object> data= Maps.newHashMap();
        OrderStatistics orderStatistics=orderStatisticsService.selectPointOrderStatisticsByPointId(pointId,startTime,endTime);
        if(orderStatistics!=null){
            data.put("orderNum",orderStatistics.getOrderNum());
            data.put("freeNum",orderStatistics.getFreeNum());
            data.put("orderAllMoney",orderStatistics.getOrderAllMoeny());
        }
        List<GoodsStatistics> list= Lists.newArrayList() ;
        //单商品销量统计
        if(index==10){
            list=goodsStatisticsService.selectGoodsStatisticsList(shopId,pointId,classifyId,startTime,endTime);
        }
        //分类商品销量统计
        if(index==20){
            list=goodsStatisticsService.selectGoodsStatisticsListByClassifyId(shopId,pointId,startTime,endTime);
        }
        data.put("list",list);
        return AjaxResult.success(data);
    }

}
